package com.fss.cmsContent.action;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Map;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;

import org.apache.struts2.ServletActionContext;

import com.fss.cmsContent.service.CmsContentService;
import com.fss.util.AbstractSimpleAction;
import com.fssDataModels.TbCmsContent;
import com.opensymphony.xwork2.ActionContext;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


public class CmsContentAction extends AbstractSimpleAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private CmsContentService cmsContentService;
	
	private TbCmsContent cmsBtn1;
	private TbCmsContent cmsBtn2;
	private TbCmsContent cmsBtn3;
	private TbCmsContent cmsTitle1;
	private TbCmsContent cmsTitle2;
	private TbCmsContent cmsTitle3;
	private TbCmsContent cmsTitle4;
	private TbCmsContent cmsTitle5;
	private TbCmsContent cmsTitle6;
	private TbCmsContent cmsTitle7;
	private TbCmsContent cmsTitle8;
	
	private File image1;
	private File image2;
	private File image3;
	
	private String image1FileName;
	private String image2FileName;
	private String image3FileName;

	
	@Override
	public String initAdd() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String doAdd() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String initEdit() throws Exception {
		
		this.getCMSContent();
		return SUCCESS;
	}

	@Override
	public String doEdit() throws Exception {
		
		List <TbCmsContent> cmsList = new ArrayList<TbCmsContent>();
		cmsList.add(this.cmsBtn1);
		cmsList.add(this.cmsBtn2);
		cmsList.add(this.cmsBtn3);
		cmsList.add(this.cmsTitle1);
		cmsList.add(this.cmsTitle2);
		cmsList.add(this.cmsTitle3);
		cmsList.add(this.cmsTitle4);
		cmsList.add(this.cmsTitle5);
		cmsList.add(this.cmsTitle6);
		cmsList.add(this.cmsTitle7);
		cmsList.add(this.cmsTitle8);
		
		if(this.image1!=null){
			/*int position = imageFileName.lastIndexOf(".");     
            String extension = imageFileName.substring(position);
			String targetDirectory = "C:/Documents and Settings/Administrator/workspace"; 
			String targetFileName ="FSS_image"+extension;
			
			File target = new File(targetDirectory, targetFileName);     
            FileUtils.copyFile(image, target); */
			
			changeImage("234.gif",170,85);
		}
		if(this.image2!=null){
			changeImage("123.jpg",116,93);
		}
		if(this.image3!=null){
			changeImage("345.jpg",116,93);
		}
		
		if(this.cmsContentService.editCMSContent(cmsList)==1){
			this.msg="アップデートしました！";
			this.getCMSContent();
			return SUCCESS;
		}else{
			this.msg="失敗しました！";
			return INPUT;
		}
		
	}

	@Override
	public String doDelete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	private void getCMSContent(){
		this.cmsContentService.getCMSContent();
		Map session = ActionContext.getContext().getSession();
		
		this.cmsBtn1 = (TbCmsContent) session.get("btn1");
		this.cmsBtn2 = (TbCmsContent) session.get("btn2");
		this.cmsBtn3 = (TbCmsContent) session.get("btn3");
		this.cmsTitle1 = (TbCmsContent) session.get("title1");
		this.cmsTitle2 = (TbCmsContent) session.get("title2");
		this.cmsTitle3 = (TbCmsContent) session.get("title3");
		this.cmsTitle4 = (TbCmsContent) session.get("title4");
		this.cmsTitle5 = (TbCmsContent) session.get("title5");
		this.cmsTitle6 = (TbCmsContent) session.get("title6");
		this.cmsTitle7 = (TbCmsContent) session.get("title7");
		this.cmsTitle8 = (TbCmsContent) session.get("title8");
	}
	
	private void changeImage(String imageName,int width, int height)throws Exception{

		Image image;
		String targetDirectory = ServletActionContext.getServletContext().getRealPath("/images");
		System.out.println(targetDirectory);
		
		try {
			if(imageName.equalsIgnoreCase("234.gif")){
				image = ImageIO.read(this.image1);
			}else if(imageName.equalsIgnoreCase("123.jpg")){
				image = ImageIO.read(this.image2);
			}else{
				image = ImageIO.read(this.image3);
			}
			
        //int width = image.getWidth(null) ;
        //int height = image.getHeight(null) ;
        File target = new File(targetDirectory, imageName);
        
        BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR) ;
        bufferedImage.getGraphics().drawImage(image, 0, 0, width, height,null) ;
        
        FileOutputStream fos = new FileOutputStream(target) ;
        
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos) ;
        
        encoder.encode(bufferedImage) ;
        
        fos.close();
        
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public CmsContentService getCmsContentService() {
		return cmsContentService;
	}

	public void setCmsContentService(CmsContentService cmsContentService) {
		this.cmsContentService = cmsContentService;
	}

	public TbCmsContent getCmsBtn1() {
		return cmsBtn1;
	}

	public void setCmsBtn1(TbCmsContent cmsBtn1) {
		this.cmsBtn1 = cmsBtn1;
	}

	public TbCmsContent getCmsBtn2() {
		return cmsBtn2;
	}

	public void setCmsBtn2(TbCmsContent cmsBtn2) {
		this.cmsBtn2 = cmsBtn2;
	}

	public TbCmsContent getCmsBtn3() {
		return cmsBtn3;
	}

	public void setCmsBtn3(TbCmsContent cmsBtn3) {
		this.cmsBtn3 = cmsBtn3;
	}

	public TbCmsContent getCmsTitle1() {
		return cmsTitle1;
	}

	public void setCmsTitle1(TbCmsContent cmsTitle1) {
		this.cmsTitle1 = cmsTitle1;
	}

	public TbCmsContent getCmsTitle2() {
		return cmsTitle2;
	}

	public void setCmsTitle2(TbCmsContent cmsTitle2) {
		this.cmsTitle2 = cmsTitle2;
	}

	public TbCmsContent getCmsTitle3() {
		return cmsTitle3;
	}

	public void setCmsTitle3(TbCmsContent cmsTitle3) {
		this.cmsTitle3 = cmsTitle3;
	}

	public TbCmsContent getCmsTitle4() {
		return cmsTitle4;
	}

	public void setCmsTitle4(TbCmsContent cmsTitle4) {
		this.cmsTitle4 = cmsTitle4;
	}

	public TbCmsContent getCmsTitle5() {
		return cmsTitle5;
	}

	public void setCmsTitle5(TbCmsContent cmsTitle5) {
		this.cmsTitle5 = cmsTitle5;
	}

	public TbCmsContent getCmsTitle6() {
		return cmsTitle6;
	}

	public void setCmsTitle6(TbCmsContent cmsTitle6) {
		this.cmsTitle6 = cmsTitle6;
	}

	public TbCmsContent getCmsTitle7() {
		return cmsTitle7;
	}

	public void setCmsTitle7(TbCmsContent cmsTitle7) {
		this.cmsTitle7 = cmsTitle7;
	}

	public TbCmsContent getCmsTitle8() {
		return cmsTitle8;
	}

	public void setCmsTitle8(TbCmsContent cmsTitle8) {
		this.cmsTitle8 = cmsTitle8;
	}

	public File getImage1() {
		return image1;
	}

	public void setImage1(File image1) {
		this.image1 = image1;
	}

	public File getImage2() {
		return image2;
	}

	public void setImage2(File image2) {
		this.image2 = image2;
	}

	public File getImage3() {
		return image3;
	}

	public void setImage3(File image3) {
		this.image3 = image3;
	}

	public String getImage1FileName() {
		return image1FileName;
	}

	public void setImage1FileName(String image1FileName) {
		this.image1FileName = image1FileName;
	}

	public String getImage2FileName() {
		return image2FileName;
	}

	public void setImage2FileName(String image2FileName) {
		this.image2FileName = image2FileName;
	}

	public String getImage3FileName() {
		return image3FileName;
	}

	public void setImage3FileName(String image3FileName) {
		this.image3FileName = image3FileName;
	}

	

	

	
	
}